package com.qf.first.controller;



import com.qf.first.entity.Shop;
import com.qf.first.entity.User;
import com.qf.first.exceptions.BaseException;
import com.qf.first.pojo.vo.RespVo;
import com.qf.first.pojo.vo.ShopUser;
import com.qf.first.service.ShopService;
import com.qf.first.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(tags = "商户模块")
@RestController
@RequestMapping("/shopUser")
public class ShopUserController extends BaseController{
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;
    @ApiOperation("查看商户是否存在")
    @GetMapping("/checkShop")
    public RespVo checkShop(Shop shop){
        if (shop==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"数据不能为空");
        }
        Shop shop1 = shopService.checkShop(shop);
        if (shop1==null){
            return convertFail("商户不存在");

        }
        return convertSuccess(shop1);
    }

    @ApiOperation("注册商户")
    @GetMapping("/shopUserRegister")
    public RespVo shopUserRegister(HttpSession session, Shop shop){
        Long id =   (Long)session.getAttribute("id");
        if (id==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"用户未登录");
        }
        Shop myShop = new Shop();
        myShop.setUserId(id);
        Shop shop1 = shopService.checkShop(myShop);
        if (shop1!=null){
            return convertFail("您已注册过商户");
        }
        shop.setUserId(id);
        int i = shopService.addShop(shop);
        if (i==0){
            return convertFail("商户注册失败");
        }
        User user = new User();
        user.setUserId(id);
        user.setRoleId(2);
        userService.updateUser(user);
        return convertSuccess(shop);
    }

    @ApiOperation("显示商户名")
    @GetMapping("/getShopName")
    public RespVo getShopName(HttpSession session, Shop shop){
        Shop myShop = new Shop();
        Long id = (Long)session.getAttribute("id");
        if (id==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"商户未登录");
        }
        myShop.setUserId(id);
        Shop shop1 = shopService.checkShop(myShop);
        if (shop1==null){
            return convertFail("商户未注册");
        }
        session.setAttribute("shopId",shop1.getShopId());
        return convertSuccess(shop1);
    }

    @ApiOperation("获取商户信息")
    @GetMapping("/getShopUser")
    public RespVo getShopUser(HttpSession session) {
        Shop myShop = new Shop();
        Long id = (Long) session.getAttribute("id");
        if (id == null) {
            throw new BaseException(RespVo.CodeType.FAIL.getValue(), "商户未登录");
        }
        myShop.setUserId(id);
        ShopUser shopUser = shopService.selectShop(myShop);
        if (shopUser == null) {
            return convertFail("商户未注册");
        }
        return convertSuccess(shopUser);
    }

    @ApiOperation("修改商户信息")
    @GetMapping("/changeShop")
    public RespVo changeShop(HttpSession session,Shop shop){
        if (shop==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(), "信息不能为空");
        }
        Long shopId = (Long) session.getAttribute("shopId");
        if (shopId==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(), "商户未登录");
        }
        shop.setShopId(shopId);
        int i = shopService.updateShop(shop);
        if (i==0){
            return convertFail("修改失败");
        }
        return convertSuccess("修改成功");
    }
}
